package com.example.test.dialogfactory;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.BigTextStyle;
import androidx.core.app.NotificationManagerCompat;

public class MainActivity110 extends AppCompatActivity {

    private Button button1;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity110.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main110 );
        initView();
    }

    private void initView() {

        button1 = (Button) findViewById( R.id.button1 );
        button1.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                createNotificationChannel();

                Intent intent = new Intent( v.getContext(), MainActivity110.class );
                intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                int flag;
                if ( VERSION.SDK_INT >= VERSION_CODES.M ) {
                    flag = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
                } else {
                    flag = PendingIntent.FLAG_UPDATE_CURRENT;
                }
                PendingIntent pendingIntent = PendingIntent.getActivity( v.getContext(), 0, intent, flag );

                NotificationCompat.Builder builder = new NotificationCompat.Builder( v.getContext(), "普通通知" )
                    .setSmallIcon( R.drawable.caodi_shangdegou003 )
                    .setContentTitle( "通知标题" )
                    .setContentText( "通知内容" )
                    .setContentIntent( pendingIntent )
                    .setAutoCancel( true )
                    .setStyle( new BigTextStyle().bigText( "曾经沧海难为水,除却巫山不是云,取次花丛懒回顾,半缘修道半缘君" ) )
                    .setPriority( NotificationCompat.PRIORITY_DEFAULT );

                NotificationManagerCompat notificationManager = NotificationManagerCompat.from( v.getContext() );

                // notificationId is a unique int for each notification that you must define
                notificationManager.notify( 12, builder.build() );
            }
        } );
    }

    private void createNotificationChannel() {

        if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel( "普通通知", "测试通知-频道", importance );
            channel.setDescription( "测试通知-频道" );
            NotificationManager notificationManager = getSystemService( NotificationManager.class );
            notificationManager.createNotificationChannel( channel );
        }
    }
}